package com.SmartCare.controller.client;

import cn.hutool.core.bean.BeanUtil;
import com.SmartCare.pojo.homeInfo.CareHomeDetails;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.SmartCare.common.Result;
import com.SmartCare.common.enums.ElderlyCareRole;
import com.SmartCare.common.vo.homeInfo.DetailVo;
import com.SmartCare.common.vo.role.NurseInfoVo;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.SmartCare.pojo.role.EmployeeElder;
import com.SmartCare.pojo.role.User;
import com.SmartCare.service.homeInfo.DetailsService;
import com.SmartCare.service.role.ElderlyInformationService;
import com.SmartCare.service.role.EmployeeElderService;
import com.SmartCare.service.role.UserService;
import com.SmartCare.service.impl.role.EmployeeElderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 护工相关接口 前端控制器
 */
@RestController
@RequestMapping("/client")
@Api(tags = "C端护工接口")
public class WorkerController {

    @Resource
    UserService userService;
    @Resource
    EmployeeElderService ers;
    @Resource
    private EmployeeElderServiceImpl employeeElderService;
    @Resource
    private ElderlyInformationService elderlyInformationService;
    @Resource
    private DetailsService detailsService;


    //护工账户登录
    @GetMapping("/login_worker")
    @Operation(summary = "护工登录", description = "护工账户进行登录")
    public Result login(@Parameter(description = "账户", required = true) @RequestParam String account,
                        @Parameter(description = "密码", required = true) @RequestParam String password,
                        @Parameter(description = "养老院名称", required = true) @RequestParam String careHomeName,
                        HttpServletRequest request) {

        return userService.workerLogin(request,account, password,careHomeName);
    }

    //获取护工对应的老人列表
    @GetMapping("/query_elderList")
    @Operation(summary = "获取老人列表", description = "获取护工对应的老人列表")
    public Result getElderList(@Parameter(description = "护工ID", required = true) @RequestParam Long workerId){

        return ers.getElderList(workerId);
    }

    /**
     * 获取护工个人信息
     */
    @GetMapping("/nurse/query")
    public Result getNurseInfo(Long userId) {
        User user = userService.getById(userId);
        if (user==null){
            return Result.error("用户不存在");
        }

        NurseInfoVo vo = new NurseInfoVo();
        vo.setNickname(user.getNickname());
        vo.setPosition(ElderlyCareRole.getRoleNameByValue(user.getRole()));
        vo.setCareHomeName(user.getCareHomeName());

        LambdaQueryWrapper<EmployeeElder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeElder::getEmployeeId,user.getUserId());
        List<EmployeeElder> list = employeeElderService.list(queryWrapper);


        //查询护工绑定老人信息
        List<Long> eldersId= new ArrayList<>();
        List<String> elderNames=new ArrayList<>();
        for (EmployeeElder employeeElder : list) {
            eldersId.add(employeeElder.getElderlyId());
        }
        for (Long aLong : eldersId) {
            elderNames.add(elderlyInformationService.getById(aLong).getElderlyName());
        }

        vo.setElders(elderNames);

        return Result.success(vo);
    }

    /**
     * 所有养老院名称查询
     */
    @GetMapping("/home/name/query")
    public Result getHomeNames(){
        List<CareHomeDetails> details = detailsService.list();
        List<DetailVo> names= new ArrayList<>();
        for (CareHomeDetails detail : details) {
            DetailVo vo = new DetailVo();
            BeanUtil.copyProperties(detail,vo);
            names.add(vo);
        }
        return Result.success("查询成功",names);
    }

    /**
     * 修改护工绑定养老院信息
     */
    @GetMapping("/home/name/update")
    public Result updateHomeName(Long userId,String careHomeName) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getUserId,userId);
        updateWrapper.set(User::getCareHomeName,careHomeName);
        boolean res = userService.update(updateWrapper);
        if (!res){
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    /**
     * 获取养老院所有职位
     */
    @GetMapping("/position/query")
    public Result positionQuery() {
        List<Map<String,Integer>> positions = new ArrayList<>();
        ElderlyCareRole[] roles = ElderlyCareRole.values();

        for (ElderlyCareRole position : roles) {
            Map<String,Integer> positionMap = new HashMap<>();
            positionMap.put( position.getRoleName(),position.getValue());
            positions.add(positionMap);
        }

        return Result.success(positions);
    }

    /**
     * 护工修改职位身份
     */
    @GetMapping("/position/update")
    public Result updatePosition(Long userId,Integer newPosition){

        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getUserId,userId);
        updateWrapper.set(User::getRole,newPosition);
        boolean res = userService.update(updateWrapper);
        if (!res){
            return Result.error("修改失败");
        }
        return Result.success();
    }

    /**
     * 养老院所有老人名称查询
     */
    @GetMapping("/elderName/query")
    public Result getElderNameAndAcc(String careHomeName) {
        //查询养老院id
        CareHomeDetails detail = detailsService.getOne(new LambdaQueryWrapper<CareHomeDetails>().eq(CareHomeDetails::getName, careHomeName));
        LambdaQueryWrapper<ElderlyInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ElderlyInformation::getCurrentCareHomeId,detail.getId());
        //根据id查询所有的老人
        List<ElderlyInformation> list = elderlyInformationService.list(queryWrapper);

        List<Map<String,Long>> names=new ArrayList<>();

        for (ElderlyInformation item : list) {
            Map<String,Long> map=new HashMap<>();
            map.put(item.getElderlyName(),item.getElderlyId());
            names.add(map);
        }
        return Result.success(names);
    }

    /**
     * 护工 与 老人绑定
     */
    @GetMapping("/bingding/add")
    public Result binding(Long userId,Long elderlyId) {
        return employeeElderService.binding(userId,elderlyId);
    }

    /**
     * 邮箱验证码方式登录
     */
    @GetMapping("/loginByMail")
    public Result loginByEmail(String name,String authCode) {
        return userService.loginByEmail(name,authCode);
    }

    /**
     * 忘记密码
     */
    @GetMapping("/newPassword")
    public Result newPassword(String name,String newPassword,String authCode){
        return userService.updatePwd(name,newPassword,authCode);
    }

    //游客->老人 身份修改
    @GetMapping("/toElderRole")
    public Result toElderRole(String name,String careHomeName,String openid,String iconUrl){
        return elderlyInformationService.toElderRole(name,careHomeName,openid,iconUrl);
    }
}

